/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.stop.manage.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.stop.manage.dto.ClosedDriveStopCarDTO;
import com.hopes.bizz.stop.manage.dto.ClosedDriveStopCarExcelDTO;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopCarEntity;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopEntity;
import com.hopes.bizz.stop.manage.mapper.ClosedDriveStopCarMapper;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopCarService;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopService;
import com.hopes.bizz.stop.manage.vo.ClosedDriveStopCarExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 园区停车位管理
 *
 * @author pig
 * @date 2024-02-18 15:59:12
 */
@Service
@RequiredArgsConstructor
public class ClosedDriveStopCarServiceImpl extends ServiceImpl<ClosedDriveStopCarMapper, ClosedDriveStopCarEntity> implements ClosedDriveStopCarService {

    private final ClosedDriveStopService closedDriveStopService;

    @Override
    public ValidateImportBO<ClosedDriveStopCarExcelDTO> validateImport(List<ClosedDriveStopCarExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedDriveStopCarExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedDriveStopCarExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedDriveStopCarEntity importDtoToEntity(ClosedDriveStopCarExcelDTO excelDto) {
        ClosedDriveStopCarEntity entity = new ClosedDriveStopCarEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public ClosedDriveStopCarExcelVO exportEntityToVo(ClosedDriveStopCarEntity entity) {
        ClosedDriveStopCarExcelVO excelVO = new ClosedDriveStopCarExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }

    @Override
    public void saveAndUpdateStopPark(ClosedDriveStopCarEntity closedDriveStopCar) {
        ClosedDriveStopEntity driveStopEntity = closedDriveStopService.getById(closedDriveStopCar.getStopParkId());
        driveStopEntity.setTotalPlace(driveStopEntity.getTotalPlace() + 1);
        driveStopEntity.setSurplusPlace(driveStopEntity.getSurplusPlace() + 1);
        closedDriveStopService.updateById(driveStopEntity);
        baseMapper.insert(closedDriveStopCar);
    }

    @Override
    public R removeBatchByIdsAndUpdatePark(ClosedDriveStopCarDTO closedDriveStopCarDTO) {
        // 已预约的不能删除
        for (Long id : closedDriveStopCarDTO.getIds()) {
            ClosedDriveStopCarEntity stopCarEntity = getById(id);
            // 是否预约
            Boolean reserveFlag = stopCarEntity.getReserveFlag();
            if (reserveFlag){
                return R.failed("不可删除已预约的车位");
            }
        }
        // 未预约的车位进行删除，更新停车场数量
        removeBatchByIds(CollUtil.toList(closedDriveStopCarDTO.getIds()));
        ClosedDriveStopEntity closedDriveStopEntity = closedDriveStopService.getById(closedDriveStopCarDTO.getStopParkId());
        closedDriveStopEntity.setTotalPlace(closedDriveStopEntity.getTotalPlace() - closedDriveStopCarDTO.getIds().length);
        closedDriveStopEntity.setSurplusPlace(closedDriveStopEntity.getSurplusPlace() - closedDriveStopCarDTO.getIds().length);
        closedDriveStopService.updateById(closedDriveStopEntity);
        return R.ok();
    }
}
